package 笔试.MiHoYo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class 四六次数最多 {
    public static int m;
    public static int n;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        m = in.nextInt();
        n = in.nextInt();
        solve();
    }

    private static void solve() {
        ArrayList<Integer> cur = new ArrayList<>();
        ArrayList<Integer> res = new ArrayList<>();
        int dept = 0;
        int maxLen = String.valueOf(n).length();
        int minLen = String.valueOf(m).length();
        int[] nums = {4, 6, (int) (m / Math.pow(10, minLen - 1))};
        System.out.println(Arrays.toString(nums));
        int len = maxLen;
        while (len >= minLen) {
            dfs(cur, res, nums, dept, len);
            if (!res.isEmpty()) {
                System.out.println(res.get(0));
                return;
            }
            len--;
        }
        System.out.println(n);
    }

    private static void dfs(ArrayList<Integer> cur, ArrayList<Integer> res, int[] nums, int dept, int len) {
        if (dept == len) {
            int curNum = 0;
            for (int i = 0; i < cur.size(); i++) {
                curNum = curNum * 10 + cur.get(i);
            }
            if (curNum > n || curNum < m) {
                return;
            }
            if (res.isEmpty()) {
                res.add(curNum);
            } else {
                if (res.get(0) < curNum) {
                    res.remove(0);
                    res.add(curNum);
                }
            }
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            cur.add(nums[i]);
            dfs(cur, res, nums, dept + 1, len);
            cur.remove(cur.size() - 1);
        }
    }
}